/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sampleServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import sampleBean.CusSessionBeanRemote;
import sampleEntity.Bill;
import sampleEntity.Customers;

/**
 *
 * @author ASUS K43E
 */
public class Staff extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Context context;
            context = new InitialContext();
            Object obj = context.lookup("remoteJNDI");
            CusSessionBeanRemote remote = (CusSessionBeanRemote) obj;
            String button = request.getParameter("btAction");
            HttpSession session = request.getSession();

            //show order room
            if (button.equals("OrderRoom")) {
                String pattern = "MM/dd/yyyy";
                SimpleDateFormat format = new SimpleDateFormat(pattern);
                Date date = new Date();
                Calendar c = Calendar.getInstance();

                try {
                    date = format.parse(format.format(c.getTime()));
                } catch (ParseException ex) {
                    Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
                }

                List list = remote.showRoom(date);
                request.setAttribute("ROOM", list);
                RequestDispatcher rd = request.getRequestDispatcher("staff.jsp");
                rd.forward(request, response);
            }//check room
            else if (button.equals("Check")) {
                String username = request.getParameter("txtUsername");
                String room = request.getParameter("txtRoom");
                String pattern = "MM/dd/yyyy";
                SimpleDateFormat format = new SimpleDateFormat(pattern);
                Date date = new Date();
                Calendar c = Calendar.getInstance();

                try {
                    date = format.parse(format.format(c.getTime()));
                } catch (ParseException ex) {
                    Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
                }

                boolean result = remote.checkRoom(room, date);
                String url = "";
                if (result) {
                    boolean result1 = remote.addRoom(username, room, date);
                    if (result1) {
                        url = "Staff?btAction=OrderRoom";
                    }
                }
                response.sendRedirect(url);
            }//show birthday
            else if (button.equals("Birthday")) {
                List list = remote.findDob();
                request.setAttribute("BIRTHDAY", list);
                RequestDispatcher rd = request.getRequestDispatcher("staff.jsp");
                rd.forward(request, response);
            }//add bill detail
            else if (button.equals("+")) {
                String pattern = "MM/dd/yyyy";
                SimpleDateFormat format = new SimpleDateFormat(pattern);
                Date date = new Date();
                Calendar c = Calendar.getInstance();

                try {
                    date = format.parse(format.format(c.getTime()));
                } catch (ParseException ex) {
                    Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
                }

                String username = request.getParameter("txtUsername");
                String productId = request.getParameter("ddlProId");
                String quantityStr = request.getParameter("txtQuantity");
                String mess = "";
                if (quantityStr.length() == 0) {
                    mess = "Quantity is required!";
                    request.setAttribute("MESS", mess);
                    RequestDispatcher rd = request.getRequestDispatcher("bill-making.jsp");
                    rd.forward(request, response);
                } else {
                    int quantity;
                    try {
                        quantity = Integer.parseInt(quantityStr);
                        if (quantity <= 0) {
                            mess = "Quantity is more than 0.";
                            request.setAttribute("MESS", mess);
                            RequestDispatcher rd = request.getRequestDispatcher("bill-making.jsp");
                            rd.forward(request, response);
                        } else {
                            boolean result = remote.addProduct(username, date, productId, quantity);
                            String url = "";
                            if (result) {
                                List list = remote.findBillItem(username, date);
                                session.setAttribute("BILLITEM", list);
                                Bill bill = remote.findBill(username, date);
                                session.setAttribute("BILL", bill);
                                Customers cus = remote.findCus(username);
                                session.setAttribute("CUS", cus);
                                session.setAttribute("DATE", date);
                                url = "bill-detail.jsp";
                            }
                            response.sendRedirect(url);
                        }
                    } catch (NumberFormatException e) {
                        mess = "Quantity is an integer.";
                        request.setAttribute("MESS", mess);
                        RequestDispatcher rd = request.getRequestDispatcher("bill-making.jsp");
                        rd.forward(request, response);
                    }
                }
            }//check card
            else if (button.equals("CheckCus")) {
                String username = request.getParameter("txtUsername");
                Customers cus = remote.findCus(username);
                session.setAttribute("CUS", cus);
                response.sendRedirect("bill-making.jsp");
            }//get bill
            else if (button.equals("getBill")) {
                String pattern = "MM/dd/yyyy";
                SimpleDateFormat format = new SimpleDateFormat(pattern);
                Date date = new Date();
                Calendar c = Calendar.getInstance();

                try {
                    date = format.parse(format.format(c.getTime()));
                } catch (ParseException ex) {
                    Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
                }

                String username = request.getParameter("txtUsername");
                List list = remote.findBillItem(username, date);
                session.setAttribute("BILLITEM", list);
                Bill bill = remote.findBill(username, date);
                session.setAttribute("BILL", bill);
                Customers cus = remote.findCus(username);
                session.setAttribute("CUS", cus);
                session.setAttribute("DATE", date.toString());
                response.sendRedirect("bill-detail.jsp");
            }//add more
            else if (button.equals("Add more")) {
                response.sendRedirect("bill-making.jsp");
            }//Print Bill
            else if (button.equals("Print Bill")) {
                String username = request.getParameter("txtUsername");
                int point = Integer.parseInt(request.getParameter("txtPoint"));
                String rank = request.getParameter("txtRank");

                boolean result = remote.printBill(username, point, rank);
                if (result) {
                    response.sendRedirect("staff.jsp");
                }
            }//DeletePro
            else if (button.equals("DeletePro")) {
                String username = request.getParameter("txtUsername");
                String productId = request.getParameter("txtProduct");

                String pattern = "MM/dd/yyyy";
                SimpleDateFormat format = new SimpleDateFormat(pattern);
                Date date = new Date();
                Calendar c = Calendar.getInstance();

                try {
                    date = format.parse(format.format(c.getTime()));
                } catch (ParseException ex) {
                    Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
                }

                boolean result = remote.deletePro(username, date, productId);
                if (result) {
                    List list = remote.findBillItem(username, date);
                    session.setAttribute("BILLITEM", list);
                    Bill bill = remote.findBill(username, date);
                    session.setAttribute("BILL", bill);
                    response.sendRedirect("bill-detail.jsp");
                }
            }
        } catch (NamingException ex) {
            Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
